/**
 * Copyright  2025, Hai Yue Xing He  ZHAO LIMIN
 *
 * @author        ZHAO LIMIN
 * @version       1.0.1
 * @since         2025-04
 * defer.ts       用于在完成时解决承诺的函数
 *
 * 参考：
 * function loadScript(url: string): Promise<void> {
 *     return new Promise((resolve, reject) => {
 *         const script = document.createElement('script');
 *         script.src = url;
 *         script.onload = resolve;
 *         script.onerror = reject;
 *         document.head.appendChild(script);
 *    });
 * }
 */

/**
 * A function used to resolve a promise upon completion .
 * 用于在完成时解决承诺的函数
 * @callback defer.resolve
 *
 * @param {*} value The resulting value.
 */

/**
 * A function used to reject a promise upon failure.
 * @callback defer.reject    (message: string, source?: string, lineno?: number, colno?: number, error?: Error) => void
 *
 * @param {*} error The error.
 */

/**
 * 包含一个promise对象的对象，用于解决或拒绝该promise。
 *
 * @typedef {object} defer.deferred
 * @property {defer.resolve} resolve Resolves the promise when called.
 * @property {defer.reject} reject Rejects the promise when called.
 * @property {Promise} promise Promise object.
 */

/**
 * 创建一个延迟对象，其中包含一个承诺对象和用于解决或拒绝该承诺的函数。
 * @returns {defer.deferred}
 * @private
 */
export function defer() {
  let resolve
  let reject
  const promise = new Promise(function (res, rej) {
    resolve = res
    reject = rej
  })

  return {
    resolve: resolve,
    reject: reject,
    promise: promise,
  }
}
